<template>
  <div class="app-container" id="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="外键，关联报销主表ID" prop="applyId">
        <el-input
          v-model="queryParams.applyId"
          placeholder="请输入外键，关联报销主表ID"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="费用发生时间" prop="occurrenceDate">
        <el-date-picker filterable clearable
          v-model="queryParams.occurrenceDate"
          type="datetime"
          value-format="yyyy-MM-dd"
          placeholder="请选择费用发生时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="费用金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入费用金额"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入申请人ID"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人部门ID" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入申请人部门ID"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- 更多查询条件-->
      <template v-if="isShowMore">


      </template>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="isShowMore?'el-icon-d-arrow-right':'el-icon-d-arrow-left'" size="mini" type="warning" @click="isShowMore = !isShowMore">更多查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['oa:reimbursement_detail:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['oa:reimbursement_detail:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['oa:reimbursement_detail:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['oa:reimbursement_detail:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reimbursement_detailList" @selection-change="handleSelectionChange" stripe :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" label="序号" align="center" fixed/>
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="外键，关联报销主表ID" align="center" prop="applyId" />
      <el-table-column label="费用类型，如：交通费、餐饮费、住宿费等" align="center" prop="expenseType" />
      <el-table-column label="费用发生时间" align="center" prop="occurrenceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.occurrenceDate, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="费用金额" align="center" prop="amount" />
      <el-table-column label="费用说明" align="center" prop="description" />
      <el-table-column label="附件URL" align="center" prop="attachmentUrl" />
      <el-table-column label="申请人ID" align="center" prop="userId" />
      <el-table-column label="申请人部门ID" align="center" prop="deptId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleInfo(scope.row)"
              v-hasPermi="['oa:reimbursement_detail:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:reimbursement_detail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oa:reimbursement_detail:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改OA报销申请-费用明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
      <el-col :span="8">
            <el-form-item label="外键，关联报销主表ID" prop="applyId">
              <el-input v-model="form.applyId"  placeholder="请输入外键，关联报销主表ID" />
            </el-form-item>
      </el-col>
          <el-col :span="8">
                <el-form-item label="费用发生时间" prop="occurrenceDate">
                  <el-date-picker filterable clearable
                    v-model="form.occurrenceDate"
                    type="datetime"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择费用发生时间">
                  </el-date-picker>
                </el-form-item>
          </el-col>
      <el-col :span="8">
            <el-form-item label="费用金额" prop="amount">
              <el-input v-model="form.amount"  placeholder="请输入费用金额" />
            </el-form-item>
      </el-col>
          <el-col :span="24">
                <el-form-item label="费用说明" prop="description">
                  <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
                </el-form-item>
          </el-col>
          <el-col :span="24">
                <el-form-item label="附件URL" prop="attachmentUrl">
                  <el-input v-model="form.attachmentUrl" type="textarea" placeholder="请输入内容" />
                </el-form-item>
          </el-col>
      <el-col :span="8">
            <el-form-item label="删除标记：0-正常，1-删除" prop="delFlag">
              <el-input v-model="form.delFlag"  placeholder="请输入删除标记：0-正常，1-删除" />
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="申请人ID" prop="userId">
              <el-input v-model="form.userId"  placeholder="请输入申请人ID" />
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="申请人部门ID" prop="deptId">
              <el-input v-model="form.deptId"  placeholder="请输入申请人部门ID" />
            </el-form-item>
      </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- OA报销申请-费用明细详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="68%" append-to-body>
      <el-descriptions border >
                        <el-descriptions-item label="外键，关联报销主表ID" >{{form.applyId}}</el-descriptions-item>
                        <el-descriptions-item label="费用发生时间" >
                            {{ parseTime(form.occurrenceDate, '{y}-{m}-{d} {h}:{i}')}}
                        </el-descriptions-item>
                        <el-descriptions-item label="费用金额" >{{form.amount}}</el-descriptions-item>
                        <el-descriptions-item label="费用说明" >{{form.description}}</el-descriptions-item>
                        <el-descriptions-item label="附件URL" >{{form.attachmentUrl}}</el-descriptions-item>
                        <el-descriptions-item label="删除标记：0-正常，1-删除" >{{form.delFlag}}</el-descriptions-item>
                        <el-descriptions-item label="申请人ID" >{{form.userId}}</el-descriptions-item>
                        <el-descriptions-item label="申请人部门ID" >{{form.deptId}}</el-descriptions-item>
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReimbursement_detail, getReimbursement_detail, delReimbursement_detail, addReimbursement_detail, updateReimbursement_detail } from "@/api/oa/reimbursement_detail";

export default {
  name: "Reimbursement_detail",
  data() {
    return {
      // 是否显示更多
      isShowMore: false,
      // el-table 高度
      tableHeight: 650, // 用于存储 el-table 的高度
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // OA报销申请-费用明细表格数据
      reimbursement_detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyId: null,
        expenseType: null,
        occurrenceDate: null,
        amount: null,
        description: null,
        attachmentUrl: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        applyId: [
          { required: true, message: "外键，关联报销主表ID不能为空", trigger: "blur" }
        ],
        expenseType: [
          { required: true, message: "费用类型，如：交通费、餐饮费、住宿费等不能为空", trigger: "change" }
        ],
        occurrenceDate: [
          { required: true, message: "费用发生时间不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "费用金额不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "申请人ID不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "申请人部门ID不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.setTableHeight();
    window.addEventListener('resize', this.setTableHeight); // 监听窗口大小变化
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setTableHeight); // 移除事件监听
  },
  methods: {
    /** 查询OA报销申请-费用明细列表 */
    getList() {
      this.loading = true;
      listReimbursement_detail(this.queryParams).then(response => {
        this.reimbursement_detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        applyId: null,
        expenseType: null,
        occurrenceDate: null,
        amount: null,
        description: null,
        attachmentUrl: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        delFlag: null,
        userId: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加OA报销申请-费用明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getReimbursement_detail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改OA报销申请-费用明细";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const id = row.id || this.ids
      getReimbursement_detail(id).then(response => {
        this.form = response.data;
        this.openInfo = true;
        this.title = "OA报销申请-费用明细详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateReimbursement_detail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReimbursement_detail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除OA报销申请-费用明细编号为"' + ids + '"的数据项？').then(function() {
        return delReimbursement_detail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/reimbursement_detail/export', {
        ...this.queryParams
      }, `OA报销申请-费用明细数据导出_${new Date().getTime()}.xlsx`)
    },
    // 设置表格高度
    setTableHeight() {
      const appContainer = document.getElementById('app-container');
      if (appContainer) {
        this.tableHeight = appContainer.clientHeight * 0.8;
      }
    }
  }
};
</script>
